package com.ebt.m.commons.utils;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import com.ebt.m.commons.BaseApplication;

/**
 * Created by wanglu on 2017/11/29.
 */

public class AdviceUtil {
    public static final String NO_APK = "NO_APK";

    public static String getApkVersionCode() {
        String localVersion = NO_APK;
        try {
            PackageInfo packageInfo = BaseApplication.getBaseApplication().getPackageManager().getPackageInfo(BaseApplication.getBaseApplication().getPackageName(), 0);
            localVersion = packageInfo.versionCode + "";
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return localVersion;
    }

    public static String getApkVersionName() {
        String localVersion = NO_APK;
        try {
            PackageInfo packageInfo = BaseApplication.getBaseApplication().getPackageManager().getPackageInfo(BaseApplication.getBaseApplication().getPackageName(), 0);
            localVersion = packageInfo.versionName + "";
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return localVersion;
    }

    /**
     * 获取meta信息
     *
     * @param context
     * @param name
     * @return
     */
    public static String getMetadata(Context context, String name) {
        try {
            ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(
                    context.getPackageName(), PackageManager.GET_META_DATA);
            if (appInfo.metaData != null) {
                return appInfo.metaData.getString(name);
            }
        } catch (PackageManager.NameNotFoundException e) {
            // if we can’t find it in the manifest, just return null
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 获取当前手机系统版本号
     *
     * @return 系统版本号
     */
    public static String getSystemVersion() {
        return "Android-" + android.os.Build.VERSION.RELEASE;
    }
}
